package vip.xiaonuo.issued.impl;

import cn.hutool.core.util.ObjectUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import vip.xiaonuo.core.pojo.httpclient.ReturnJson;
import vip.xiaonuo.core.util.IpAddressUtil;
import vip.xiaonuo.core.util.OkHttpUtils;


import java.util.HashMap;
import java.util.Map;

import vip.xiaonuo.facedoor.iservice.IFaceDoorDtoService;
import vip.xiaonuo.facedoor.rule.RuleConstant;
import vip.xiaonuo.facedoor.vo.dewo.AddPersonRequest;
import vip.xiaonuo.facedoor.vo.dewo.DeletePersonRequest;
import vip.xiaonuo.facedoor.vo.dewo.DeviceStateResponse;
import vip.xiaonuo.facedoor.vo.dewo.GetPersonRequest;
import vip.xiaonuo.facedoor.vo.dewo.GetPersonResponse;
import vip.xiaonuo.facedoor.vo.dewo.UpdatePersonRequest;
import vip.xiaonuo.facedoor.vo.haiqing.BaseResponse;
import vip.xiaonuo.facedoor.vo.haiqing.GetAndSetDoorCondition;
import vip.xiaonuo.facedoor.vo.haiqing.GetAndSetGeneralParamRequest;
import vip.xiaonuo.facedoor.vo.haiqing.GetAndSetSoundRequest;
import vip.xiaonuo.facedoor.vo.haiqing.OpenDoorRequest;
import vip.xiaonuo.facedoor.vo.haiqing.RebootDeviceRequest;
import vip.xiaonuo.facedoor.vo.haiqing.RequestHeader;
import vip.xiaonuo.facedoor.vo.haiqing.SetFactoryDefaultRequest;
import vip.xiaonuo.facedoor.vo.haiqing.SetSubscribeRequest;
import vip.xiaonuo.facedoor.vo.haiqing.SetSysTimeRequest;
import vip.xiaonuo.facedoor.vo.haiqing.SetUserPwdRequest;

@Service(RuleConstant.DW + RuleConstant.FACEDOOR_DTO)
public class HttpFaceDoorDtoServiceImpl implements IFaceDoorDtoService {

    @Override
    public DeviceStateResponse getDeviceState(String uri) throws Exception {
        Map<String, Object> param = new HashMap<>();
        param.put("key", "ABCD1234");
        return OkHttpUtils.requestDevice(OkHttpUtils.ReqType.GET, uri.concat("/device_state"), param, DeviceStateResponse.class);
    }

    @Override
    public ReturnJson addPerson(String uri, AddPersonRequest addPersonRequest) throws Exception {
        Map<String, Object> param = new HashMap<>();
        param.put("key", "ABCD1234");
        param.put("personnelId", addPersonRequest.getPersonnelId());
        param.put("name", addPersonRequest.getName());
        if (StringUtils.isNotBlank(addPersonRequest.getIdCardNum())) {
            param.put("idCardNum", addPersonRequest.getIdCardNum());
        }
        param.put("company", addPersonRequest.getCompany());
        param.put("icNum", addPersonRequest.getIcNum());
        param.put("jobNum", addPersonRequest.getJobNum());
        // 如果特征为不为空，那么只传特征，不用传图片，以此减少终端的工作
        if (!StringUtils.isEmpty(addPersonRequest.getFaceFeature())) {
            param.put("faceFeature", addPersonRequest.getFaceFeature());
        } else {
            if (StringUtils.isNotBlank(addPersonRequest.getFaceFeatureImgPath())) {
                param.put("faceFeatureImgPath", IpAddressUtil.getImageAddIp(addPersonRequest.getFaceFeatureImgPath()));
            }
        }
        if (!StringUtils.isEmpty(addPersonRequest.getFingerVeinFeature())) {
            param.put("fingerVeinFeature", addPersonRequest.getFingerVeinFeature());
        }
        param.put("wiegandNum", addPersonRequest.getWiegandNum());
        param.put("personnelType", addPersonRequest.getPersonnelType());
        param.put("floors", addPersonRequest.getFloors());
        if (StringUtils.isNotEmpty(addPersonRequest.getExpiryTime())) {
            param.put("expiryTime", addPersonRequest.getExpiryTime());
        }
        if (StringUtils.isNotBlank(addPersonRequest.getLockerData())) {
            param.put("lockerData", addPersonRequest.getLockerData());
        }
        return OkHttpUtils.requestDevice(OkHttpUtils.ReqType.GET, uri.concat("/add_personnel"), param, ReturnJson.class);
    }

    @Override
    public ReturnJson deletePerson(String uri, DeletePersonRequest deletePersonRequest) throws Exception {
        Map<String, Object> delMap = new HashMap<>();
        delMap.put("key", "ABCD1234");
        if (StringUtils.isNotBlank(deletePersonRequest.getPersonnelId())) {
            delMap.put("personnelId", deletePersonRequest.getPersonnelId());
        }
        return OkHttpUtils.requestDevice(OkHttpUtils.ReqType.GET, uri.concat("/delete_personnel"), delMap, ReturnJson.class);
    }

    @Override
    public ReturnJson deleteAllPerson(String uri, DeletePersonRequest deletePersonRequest) throws Exception {
        Map<String, Object> delMap = new HashMap<>();
        delMap.put("key", "ABCD1234");
        if (StringUtils.isNotBlank(deletePersonRequest.getDeleteAll())) {
            delMap.put("deleteAll", deletePersonRequest.getDeleteAll());
        }
        return OkHttpUtils.requestDevice(OkHttpUtils.ReqType.GET, uri.concat("/delete_personnel"), delMap, ReturnJson.class);
    }

    @Override
    public ReturnJson updatePerson(String uri, UpdatePersonRequest updatePersonRequest) throws Exception {
        Map<String, Object> param = new HashMap<>();
        param.put("key", "ABCD1234");
        param.put("personnelId", updatePersonRequest.getPersonnelId());
        param.put("name", updatePersonRequest.getName());
        if (StringUtils.isNotEmpty(updatePersonRequest.getIdCardNum())) {
            param.put("idCardNum", updatePersonRequest.getIdCardNum());
        }
        param.put("company", updatePersonRequest.getCompany());
        param.put("icNum", updatePersonRequest.getIcNum());
        param.put("personnelType", updatePersonRequest.getPersonnelType());
        param.put("jobNum", updatePersonRequest.getJobNum());
        if (StringUtils.isNotBlank(updatePersonRequest.getFaceFeatureImgPath())) {
            param.put("faceFeatureImgPath", IpAddressUtil.getImageAddIp(updatePersonRequest.getFaceFeatureImgPath()));
        }
        param.put("floors", updatePersonRequest.getFloors());
        param.put("wiegandNum", updatePersonRequest.getWiegandNum());
        if (StringUtils.isNotEmpty(updatePersonRequest.getExpiryTime())) {
            param.put("expiryTime", updatePersonRequest.getExpiryTime());
        }
        if (StringUtils.isNotBlank(updatePersonRequest.getLockerData())) {
            param.put("lockerData", updatePersonRequest.getLockerData());
        }
        return OkHttpUtils.requestDevice(OkHttpUtils.ReqType.GET, uri.concat("/update_personnel"), param, ReturnJson.class);
    }

    @Override
    public GetPersonResponse getPerson(String uri, GetPersonRequest getPersonRequest) throws Exception {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("queryType", "1");
        queryMap.put("key", "ABCD1234");
        queryMap.put("personnelId", getPersonRequest.getPersonnelId());
        return OkHttpUtils.requestDevice(OkHttpUtils.ReqType.GET, uri.concat("/query_personnels"), queryMap, GetPersonResponse.class);
    }

    @Override
    public GetPersonResponse listPerson(String uri, GetPersonRequest request) throws Exception {
        Map<String, Object> param = new HashMap<>();
        param.put("key", "ABCD1234");
        if (ObjectUtil.equal(request.getQueryType(), "2")) {
            param.put("queryType", "2");
            param.put("page", request.getPage());
            param.put("limit", request.getLimit());
        } else if (ObjectUtil.equal(request.getQueryType(), "3")) {
            param.put("name", request.getName());
            param.put("queryType", "3");
        }

        return OkHttpUtils.requestDevice(OkHttpUtils.ReqType.GET, uri.concat("/query_personnels"), param, GetPersonResponse.class);
    }

    @Override
    public BaseResponse setUserPwd(String uri, SetUserPwdRequest setUserPwdRequest, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public BaseResponse setFactoryDefault(String uri, SetFactoryDefaultRequest setFactoryDefaultRequest, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public BaseResponse setSysTime(String uri, SetSysTimeRequest setSysTimeRequest, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public BaseResponse openDoor(String uri, OpenDoorRequest openDoorRequest, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public GetAndSetGeneralParamRequest getGeneralParam(String uri, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public BaseResponse setGeneralParam(String uri, GetAndSetGeneralParamRequest getAndSetGeneralParamRequest, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public BaseResponse rebootDevice(String uri, RebootDeviceRequest rebootDeviceRequest, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public GetAndSetDoorCondition getDoorCondition(String uri, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public BaseResponse setDoorCondition(String uri, GetAndSetDoorCondition getAndSetDoorCondition, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public GetAndSetSoundRequest getSound(String uri, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public BaseResponse setSound(String uri, GetAndSetSoundRequest getAndSetSoundRequest, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public BaseResponse setSubscribe(String uri, SetSubscribeRequest setSubscribeRequest, RequestHeader requestHeader) throws Exception {
        return null;
    }
}
